package cn.itcast.wushangxiantu.BaseAdapter;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import cn.itcast.wushangxiantu.Activity.Login;
import cn.itcast.wushangxiantu.MessageEvent;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import cn.itcast.wushangxiantu.UserData.User;
import cn.itcast.wushangxiantu.UserData.UserBag;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class BagGriveViewAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private UserBag userBag;
    int num = 0;//代表当前是什么装备还是道具还是灵石
    int num1 = 0;//选中的第几个格子
    int wpnumber = 0;//要出售多少物品
    int wpprice = 0;//单个价格为多少

    public BagGriveViewAdapter(Context context, UserBag userBag) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.userBag = userBag;
    }

    @Override
    public int getCount() {
        if (num == 0) {
            if (userBag == null || userBag.getBeibao().get(0) == null) {
                return 0;
            } else {
                return userBag.getBeibao().get(0).getZhuangbei().size();
            }

        } else if (num == 1) {
            if (userBag == null || userBag.getBeibao().get(1) == null) {
                return 0;
            } else {
                return userBag.getBeibao().get(1).getDaoju().size();
            }
        } else {
            if (userBag == null || userBag.getBeibao().get(2) == null) {
                return 0;
            } else {
                return userBag.getBeibao().get(2).getLingshi().size();
            }
        }
    }

    @Override
    public Object getItem(int i) {

        if (num == 0) {
            return userBag.getBeibao().get(0).getZhuangbei().get(i);
        } else if (num == 1) {
            return userBag.getBeibao().get(1).getDaoju().get(i);
        } else {
            return userBag.getBeibao().get(2).getLingshi().get(i);
        }
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("NewApi")
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            view = layoutInflater.inflate(R.layout.griveview_bag, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        if (num == 0) {
            viewHolder.tvName.setText(userBag.getBeibao().get(0).getZhuangbei().get(i).getWpname());
            viewHolder.tvNumber.setText(userBag.getBeibao().get(0).getZhuangbei().get(i).getWpnumber() + "");
            viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.heise));
            if (userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() > 290) {
                viewHolder.tvName.setTextColor(ContextCompat.getColor(context,R.color.wuqi4));
            } else if (userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() > 260) {
                viewHolder.tvName.setTextColor(ContextCompat.getColor(context,R.color.wuqi3));
            } else if (userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() > 200) {
                viewHolder.tvName.setTextColor(ContextCompat.getColor(context,R.color.wuqi2));
            } else {
                viewHolder.tvName.setTextColor(ContextCompat.getColor(context,R.color.wuqi1));
            }
        } else if (num == 1) {
            viewHolder.tvName.setText(userBag.getBeibao().get(1).getDaoju().get(i).getWpname());
            viewHolder.tvNumber.setText(userBag.getBeibao().get(1).getDaoju().get(i).getWpnumber() + "");
            viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.heise));
            if (userBag.getBeibao().get(1).getDaoju().get(i).getWpid() <= 4) {
                viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.buyu));
            } else if (userBag.getBeibao().get(1).getDaoju().get(i).getWpid() <= 8) {
                viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.kuangqu));
            } else if (userBag.getBeibao().get(1).getDaoju().get(i).getWpid() <= 12) {
                viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.linqu));
            } else if (userBag.getBeibao().get(1).getDaoju().get(i).getWpid() <= 16) {
                viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.caiji));
            } else if (userBag.getBeibao().get(1).getDaoju().get(i).getWpid() == 27) {
                viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.lingshi5));
            } else if (userBag.getBeibao().get(1).getDaoju().get(i).getWpid() <= 26) {
                viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.lingshi6));
            } else {
                viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.heise));
            }
        } else {
            viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.heise));
            viewHolder.tvName.setText(userBag.getBeibao().get(2).getLingshi().get(i).getWpname());
            viewHolder.tvNumber.setText(userBag.getBeibao().get(2).getLingshi().get(i).getWpnumber() + "");
            viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.heise));
            switch (userBag.getBeibao().get(2).getLingshi().get(i).getJsdj()) {
                case 1:
                case 2:
                case 3:
                    viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.lingshi1));
                    break;
                case 4:
                case 5:
                case 6:
                    viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.lingshi2));
                    break;
                case 7:
                case 8:
                case 9:
                    viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.lingshi3));
                    break;
                case 10:
                case 11:
                case 12:
                    viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.lingshi4));
                    break;
                case 13:
                case 14:
                case 15:
                    viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.lingshi5));
                    break;
                case 16:
                case 17:
                case 18:
                    viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.lingshi6));
                    break;
                case 19:
                case 20:
                case 21:
                    viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.lingshi7));
                    break;
                case 22:
                case 23:
                case 24:
                    viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.lingshi8));
                    break;
                case 25:
                    viewHolder.tvName.setTextColor(ContextCompat.getColor(context, R.color.lingshi9));
                    break;
            }

        }
        viewHolder.tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = "";
                switch (num) {
                    case 0:
                        content = "武器名称；" + userBag.getBeibao().get(0).getZhuangbei().get(i).getWpname() + "\n" +
                                "使用等级：" + userBag.getBeibao().get(0).getZhuangbei().get(i).getSydj() + "\n******装备属性******\n";
                        switch (userBag.getBeibao().get(0).getZhuangbei().get(i).getWpregion()) {
                            case 2:
                                content += "攻击力：" + ((int) (userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * 40 +
                                        userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * ((double) userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() / 50))) + "\n";
                                if (((int) (userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() / 5 +
                                        userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * ((double) userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() / 1000))) > 45) {
                                    content += "暴击率：45\n";
                                } else {
                                    content += "暴击率：" + ((int) (userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() / 5 +
                                            userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * ((double) userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() / 1000))) + "\n";

                                }
                                break;
                            case 3:
                                content += "生命：" + ((int) (userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * 100 +
                                        userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * ((double) userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() / 50))) + "\n";
                                content += "防御力：" + ((int) (userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * 20 +
                                        userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * ((double) userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() / 100))) + "\n";
                                break;
                            case 4:
                                if (((int) (userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() / 5 +
                                        userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * ((double) userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() / 1000))) > 45) {
                                    content += "命中：45\n";
                                } else {
                                    content += "命中：" + ((int) (userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() / 5 +
                                            userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * ((double) userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() / 1000))) + "\n";

                                }
                                content += "穿透：" + ((int) (userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * 10 +
                                        userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * ((double) userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() / 50))) + "\n";
                                break;
                            case 5:
                                if (((int) (userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() / 5 +
                                        userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * ((double) userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() / 1000))) > 45) {
                                    content += "闪避：45\n";
                                } else {
                                    content += "闪避：" + ((int) (userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() / 5 +
                                            userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * ((double) userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() / 1000))) + "\n";

                                }
                                content += "恢复：" + ((int) (userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * 10 +
                                        userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * ((double) userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() / 50))) + "\n";
                                break;
                        }
                        if (userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() > 290) {
                            content += "品质：传说\n";
                        } else if (userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() > 260) {
                            content += "品质：史诗\n";
                        } else if (userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() > 200) {
                            content += "品质：精良\n";
                        } else {
                            content += "品质：普通\n";
                        }
                        content += "售价：" + (int) (userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * userBag.getBeibao().get(0).getZhuangbei().get(i).getJsdj() * 5 + ((double) userBag.getBeibao().get(0).getZhuangbei().get(i).getPz() / 25));
                        GeneralDialogZhuangbei(userBag.getBeibao().get(0).getZhuangbei().get(i).getZuoyong(), content, i);
                        break;
                    case 1:
                        content = "道具名称：" + userBag.getBeibao().get(1).getDaoju().get(i).getWpname() + "\n******属性******\n" +
                                "说明：" + userBag.getBeibao().get(1).getDaoju().get(i).getZuoyong() + "\n售价：1";
                        GeneralDialogDaoju(" 道具", content, i);
                        break;
                    case 2:
                        content = "灵石名称：" + userBag.getBeibao().get(2).getLingshi().get(i).getWpname() + "\n******属性******\n" +
                                "说明：" + userBag.getBeibao().get(2).getLingshi().get(i).getZuoyong() + "\n******装备属性******\n" +
                                "生命：" + userBag.getBeibao().get(2).getLingshi().get(i).getJsdj() * 150 + "\n" +
                                "防御力：" + userBag.getBeibao().get(2).getLingshi().get(i).getJsdj() * 20 + "\n" +
                                "攻击力：" + userBag.getBeibao().get(2).getLingshi().get(i).getJsdj() * 40 + "\n" +
                                "恢复：" + userBag.getBeibao().get(2).getLingshi().get(i).getJsdj() * 10 + "\n" +
                                "穿透：" + userBag.getBeibao().get(2).getLingshi().get(i).getJsdj() * 10 + "\n";
                        GeneralDialogLingshi(" 灵石", content, i);
                        break;
                }
                num1 = i;
            }
        });
        return view;
    }

    public void GeneralDialogDaoju(String head, String content, final int num) {
        /**
         * 普通对话框
         * 1标题
         * 2内容
         */
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(head)

                .setMessage(content)
                .setNeutralButton("出售", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        GeneralDialog(context, "输入数量最大为:" + userBag.getBeibao().get(1).getDaoju().get(num).getWpnumber(), num);
                    }
                })
                .setNegativeButton("上架", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        GeneralDialogZahuo(context, "输入数量最大为：" + userBag.getBeibao().get(1).getDaoju().get(num).getWpnumber(), num);
                    }
                })
                .setPositiveButton("整理", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        getZhengLi(Login.getUser().getUsernumber(),userBag.getBeibao().get(1).getDaoju().get(num).getBag_id());
                    }
                });;
        dialog = builder.create();
        dialog.show();
    }

    private void getZhengLi(String usernumber, int wpid) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + usernumber + "&need=zhengliBag" +
                "&bag_id=" + wpid, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("zhengli", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("zhengli", src);
                UserBag user = null;
                try {
                    user = new Gson().fromJson(src, UserBag.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = user;
                message.what = 5;
                handler.sendMessage(message);
            }
        });
    }
    public void GeneralDialogZhuangbei(String head, String content, final int num) {
        /**
         * 普通对话框
         * 1标题
         * 2内容
         */
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(head)

                .setMessage(content)
                .setPositiveButton("装备", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        BagZhuangBei(userBag.getBeibao().get(0).getZhuangbei().get(num).getBag_id());
                        dialogInterface.dismiss();
                    }
                })
                .setNeutralButton("出售", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String content = "该物品售价为：" + (int) (userBag.getBeibao().get(0).getZhuangbei().get(num).getJsdj() * userBag.getBeibao().get(0).getZhuangbei().get(num).getJsdj() * 5 + (double) (userBag.getBeibao().get(0).getZhuangbei().get(num).getPz() / 25));
                        GeneralDialog("确定出售吗？", content, num, 1, 1);
                    }
                })
                .setNegativeButton("上架", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        wpnumber = 1;
                        GeneralDialogDingJia(context, userBag.getBeibao().get(0).getZhuangbei().get(num).getBag_id());
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    public void GeneralDialogLingshi(String head, String content, final int num) {
        /**
         * 普通对话框
         * 1标题
         * 2内容
         */
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(head)

                .setMessage(content)
                .setNegativeButton("上架", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        GeneralDialogZahuo(context, "最大数量为" + userBag.getBeibao().get(2).getLingshi().get(num).getWpnumber(), num);
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    public void GeneralDialog(String head, String content, final int num, final int num1, final int number) {
        /**
         * 普通对话框
         * 出售二次确认
         * 1标题
         * 2内容
         */
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(head)
                .setMessage(content)
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        switch (num1) {
                            case 1:
                                BagChuShou(1, userBag.getBeibao().get(0).getZhuangbei().get(num).getBag_id());
                                break;
                            case 2:
                                BagChuShou(number, userBag.getBeibao().get(1).getDaoju().get(num).getBag_id());
                                break;
                        }
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    public static
    class ViewHolder {
        public View rootView;
        public TextView tvName;
        public TextView tvNumber;
        public RelativeLayout li;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tvName = (TextView) rootView.findViewById(R.id.tvName);
            this.tvNumber = (TextView) rootView.findViewById(R.id.tvNumber);
            this.li = (RelativeLayout) rootView.findViewById(R.id.li);

        }

    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    private void BagChuShou(int number, int bag_id) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userName=" + Login.getUser().getUsernumber() + "&need=chushou" +
                "&bag_id=" + bag_id + "&number=" + number + "&passWord=" + Login.getUser().getPassword() + "&banben=" + new Login().getVerName(context), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("bagchushou1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("bagchushou2", src);
                UserBag user = null;
                try {
                    user = new Gson().fromJson(src, UserBag.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = user;
                message.what = 2;
                handler.sendMessage(message);
            }
        });

    }

    private void BagShangjia(int number, int bag_id, int price) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userName=" + Login.getUser().getUsernumber() + "&need=shangjia" +
                "&bag_id=" + bag_id + "&number=" + number + "&price=" + price, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("shangjia2", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("shangjia4", src);
                UserBag user = null;
                try {
                    user = new Gson().fromJson(src, UserBag.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = user;
                message.what = 4;
                handler.sendMessage(message);
            }
        });
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            if (message.what == 1) {
                User user = (User) message.obj;
                if (user != null) {
                    ToastUtil.toast(context, user.getStw());
                    if (user.getStw().equals("Yes")) {
                        Login.getUser().setHj2(user.getHj2());
                        Login.getUser().setWq2(user.getWq2());
                        Login.getUser().setJz2(user.getJz2());
                        Login.getUser().setZj2(user.getZj2());
                        Login.getUser().setZhuangbei(user.getZhuangbei());
                        userBag.getBeibao().get(0).getZhuangbei().remove(num1);
                    }
                } else {
                    ToastUtil.toast(context, "服务器不行了");
                }
            } else if (message.what == 2) {
                UserBag userBag1 = (UserBag) message.obj;
                if (userBag1 != null) {
                    if (userBag1.getStw() == null) {
                        Login.getUser().setMoney(userBag1.getBeibao().get(3).getMoney());
                        userBag.setBeibao(userBag1.getBeibao());
                        ToastUtil.toast(context, "出售成功");
                        EventBus.getDefault().post(new MessageEvent("setmoney"));
                    } else {
                        ToastUtil.toast(context, userBag1.getStw());
                    }
                } else {
                    ToastUtil.toast(context, "服务器不行了");
                }
            } else if (message.what == 4) {
                UserBag userBag1 = (UserBag) message.obj;
                if (userBag1 != null) {
                    if (userBag1.getStw() == null) {
                        userBag.setBeibao(userBag1.getBeibao());
                        ToastUtil.toast(context, "上架成功");
                    } else {
                        ToastUtil.toast(context, userBag1.getStw());
                    }
                } else {
                    ToastUtil.toast(context, "服务器炸了");
                }
            }else if (message.what == 5) {
                UserBag userBag1 = (UserBag) message.obj;
                if (userBag1 != null) {
                    if (userBag1.getStw() == null) {
                        userBag.setBeibao(userBag1.getBeibao());
                    } else {
                        ToastUtil.toast(context, userBag1.getStw());
                    }
                } else {
                    ToastUtil.toast(context, "服务器炸了");
                }
            }
            EventBus.getDefault().post(new MessageEvent("Bag"));
            return false;
        }
    });


    private void GeneralDialogZahuo(final Context context, String title, final int num1) {
        /**
         * 编辑框
         * 1标题
         */
        AlertDialog dialog;
        final EditText editText = new EditText(context);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        InputFilter[] filters = {new InputFilter.LengthFilter(9)};
        editText.setFilters(filters);
        final int content = 0;
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(title)
                .setView(editText)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (editText.getText().toString().trim().equals("")) {
                            dialogInterface.dismiss();
                            ToastUtil.toast(context, "输入有误");
                            return;
                        }
                        int chushouNum = Integer.parseInt(editText.getText().toString().trim());
                        int cunzaiNum;
                        int bag_id;
                        if (num == 1) {
                            cunzaiNum = userBag.getBeibao().get(1).getDaoju().get(num1).getWpnumber();
                            bag_id = userBag.getBeibao().get(1).getDaoju().get(num1).getBag_id();
                        } else {
                            cunzaiNum = userBag.getBeibao().get(2).getLingshi().get(num1).getWpnumber();
                            bag_id = userBag.getBeibao().get(2).getLingshi().get(num1).getBag_id();
                        }
                        if (chushouNum < 1 || chushouNum > cunzaiNum) {
                            ToastUtil.toast(context, "数量有误");
                        } else {
                            wpnumber = chushouNum;
                            GeneralDialogDingJia(context, bag_id);
                        }
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    private void GeneralDialog(final Context context, String title, final int num) {
        /**
         * 编辑框
         * 1标题
         */
        AlertDialog dialog;
        final EditText editText = new EditText(context);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        InputFilter[] filters = {new InputFilter.LengthFilter(9)};
        editText.setFilters(filters);
        final int content = 0;
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(title)
                .setView(editText)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (editText.getText().toString().trim().equals("")) {
                            int chushouNum = 0;
                            dialogInterface.dismiss();
                            ToastUtil.toast(context, "输入有误");
                            return;
                        }
                        int chushouNum = Integer.parseInt(editText.getText().toString().trim());
                        int cunzaiNum = userBag.getBeibao().get(1).getDaoju().get(num).getWpnumber();
                        if (chushouNum < 1 || chushouNum > cunzaiNum) {
                            ToastUtil.toast(context, "数量有误");
                        } else {
                            GeneralDialog("确定出售这些物品吗", "可获得：" + editText.getText().toString() + "金币", num, 2, Integer.parseInt(editText.getText().toString().trim()));
                        }
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    private void BagZhuangBei(int bag_id) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + Login.getUser().getUsernumber() + "&need=zhuangbei" +
                "&bag_id=" + bag_id + "&passWord=" + Login.getUser().getPassword() + "&banben=" + new Login().getVerName(context), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("bag1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("bag2", src);
                User user = null;
                try {
                    user = new Gson().fromJson(src, User.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = user;
                handler.sendMessage(message);
                message.what = 1;
            }
        });
    }

    private void GeneralDialogDingJia(final Context context, final int bag_id) {
        /**
         * 编辑框
         * 1标题
         */
        AlertDialog dialog;
        final EditText editText = new EditText(context);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        InputFilter[] filters = {new InputFilter.LengthFilter(9)};
        editText.setFilters(filters);
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle("输入价格5-999999999")
                .setView(editText)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (editText.getText().toString().trim().equals("") || editText.getText().length() >= 16) {
                            dialogInterface.dismiss();
                            ToastUtil.toast(context, "输入有误");
                            return;
                        }
                        int chushouNum = Integer.parseInt(editText.getText().toString().trim());
                        if (chushouNum < 5) {
                            ToastUtil.toast(context, "价格有误");
                        } else {
                            BagShangjia(wpnumber, bag_id, chushouNum);
                        }
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

}
